import requests
import parsel
import time


def GetResponse(html):
 headers = {
'user-agent': 'Mozilla/5.e (Windons NI 18.8; Winb4;x54)AppleWebkit/537.36(KHIML,like Gecko) Chrome/113.0.0.8 Safari/537.36', 'x-requested-with': 'XMLiIttpHequest',
}
 response = requests.get(url = html,headers=headers)
 html_data = response.text
 return html_data

def Getchapter(NoveId): #NoveId 传入小说id
 link = f'http://www.biquge5200.cc/{NoveId}/'
 html_data = GetResponse(html=link)
 selector = parsel.Selector(html_data)
 name = selector.css('#info h1::text').get()#小说名
 chapter_list = selector.css('#list dd a::attr(href)').getall()[9:]
 #http://www.biquge5200.cc/148_148106/178122853.html
 chapter_list = ['http://www.biquge5200.cc'+ i for i in chapter_list]
 return name,chapter_list


def GetContent(html):
 
 html_data = GetResponse(html)
 selector = parsel.Selector(html_data)
 title = selector.css('.bookname h1::text').get()
 content = '\n'.join(selector.css('#content p::text').getall())
 return title,content

def Save(name,title,content):
 with open(f'{name}.txt',mode='a',encoding='utf-8') as f:
  f.write(title)
  f.write('\n')
  f.write(content)
  f.write('\n')



#title,content = GetContent(html='http://www.biquge5200.cc/148_148106/178122853.html')
#Save(title,content)
name,chapter_list = Getchapter(NoveId='148_148106')
for chapter in chapter_list:
 time.sleep(0.5)
 title,content = GetContent(html=chapter)
 Save(name,title,content)
 print(title)
